package com.terren.webdig.setting.controller;

import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.terren.webdig.basic.BasicControllerSupport;
import com.terren.webdig.common.util.CommonUtil;
import com.terren.webdig.common.util.JsonUtil;
import com.terren.webdig.common.util.StringUtil;
import com.terren.webdig.setting.entity.LoginUser;
import com.terren.webdig.setting.service.IUserConfigService;

@Controller
@RequestMapping(value = "/userConfig")
public class UserConfigController extends BasicControllerSupport {

	@Resource(name = "userConfigService")
	protected IUserConfigService userConfigService;

	@RequestMapping(value = "/getUserList", method = RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String getUserList(HttpServletRequest request, HttpSession session, HttpServletResponse response) {
		LoginUser loginUser = (LoginUser) CommonUtil.getParameterObject(LoginUser.class, request);
		List<LoginUser> list;
		String result = ERROR;
		try {
			list = userConfigService.getUserList(loginUser);
			result = JsonUtil.pagingJson(list);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("UserConfigController.getUserList error", e);
		}
		return result;
	}

	@RequestMapping(value = "/getUserTypeList", method = RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String getUserTypeList(HttpServletRequest request, HttpSession session, HttpServletResponse response) {
		LoginUser loginUser = (LoginUser) CommonUtil.getParameterObject(LoginUser.class, request);
		String result = ERROR;
		try {
			List<LoginUser> list = userConfigService.getUserTypeList(loginUser);
			result = JsonUtil.pagingJson(list);
		} catch (Exception e) {
			e.printStackTrace();
			log.error("UserConfigController.getUserTypeList error", e);
		}
		return result;
	}

	/**
	 * 根据用户id获取用户信息
	 * 
	 * @param request
	 * @param session
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/getUserById", method = RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String getUserById(HttpServletRequest request, HttpSession session, HttpServletResponse response) {
		LoginUser loginUser = (LoginUser) CommonUtil.getParameterObject(LoginUser.class, request);
		String result = ERROR;
		try {
			LoginUser userList = userConfigService.getUserByIdAll(loginUser);
			result = JsonUtil.toJSONString(userList);
		} catch (Exception e) {
			log.error("UserConfigController.getUserById error", e);
		}
		return result;
	}

	/**
	 * 添加用户
	 * @param request
	 * @param session
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/saveUser", method = RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String saveUser(String userRole, HttpServletRequest request, HttpSession session, HttpServletResponse response) {
		String result = "";
		LoginUser loginUser = (LoginUser) CommonUtil.getParameterObject(LoginUser.class, request);
		loginUser.setRoleIds(userRole);
		try {
			userConfigService.saveUser(loginUser);
			result =JsonUtil.messageJson(true, "添加成功");
		} catch (Exception e) {
			result = JsonUtil.messageJson(false, "添加失败");
			log.error("UserConfigController.saveUser error", e);
		}
		return result;
	}

	/**
	 * 删除用户 
	 * @param request
	 * @param session
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/deleteUser", method = RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String deleteUser(HttpServletRequest request, HttpSession session, HttpServletResponse response) {
		LoginUser param = (LoginUser)CommonUtil.getParameterObject(LoginUser.class, request);
		LoginUser loginUser = (LoginUser) request.getSession().getAttribute("loginUser");// 获取登录用户
		List<LoginUser> count = null;
		String result="";
		try {
			// 如果登录用户要删除自己，弹出提示无法操作
			if (param.getUserId() == loginUser.getUserId()) {
				result = JsonUtil.messageJson(false, "登录用户与删除对象一致，无法执行删除操作！");
			} else {
				count = userConfigService.deleteUser(param);
				JsonUtil.pagingJson(count);
				result = JsonUtil.messageJson(true, "删除成功");
			}

		} catch (Exception e) {
			result = JsonUtil.messageJson(false, "删除失败,服务器异常");
			e.printStackTrace();
			log.error("UserConfigController.deleteUser", e);
		}
		return result;
	}

	/**
	 * 更新用户信息(先删除原来信息再重新保存)
	 * @param request
	 * @param session
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/updateUser", method = RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String updateUser(String userRole, HttpServletRequest request, HttpSession session, HttpServletResponse response) {
		String result="";
		LoginUser loginUser = (LoginUser)CommonUtil.getParameterObject(LoginUser.class, request);
		loginUser.setRoleIds(userRole);
		try {
			userConfigService.deleteUser(loginUser);
			userConfigService.saveUser(loginUser);
			result = JsonUtil.messageJson(true, "修改成功");
		} catch (Exception e) {
			result = JsonUtil.messageJson(false, "服务器异常，修改失败");
			log.error("UserConfigController.updateUser error", e);
		}
		return result;
	}
	
	@RequestMapping(value = "/getUserOption", method = RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String getUserOption(HttpServletRequest request, HttpSession session, HttpServletResponse response) {
		String result="";
		LoginUser loginUser = (LoginUser)CommonUtil.getParameterObject(LoginUser.class, request);
		try {
			List<LoginUser> loginUserlist = userConfigService.getUserOption(loginUser);
			result = JsonUtil.pagingJson(loginUserlist);
		} catch (Exception e) {
			log.error("UserConfigController.getUserOption error", e);
		}
		return result;
	}

	/**
	 * 修改密码
	 * @param request
	 * @param session
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/editPassword", method = RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String editPassword(HttpServletRequest request, HttpSession session, HttpServletResponse response) {
		String result="";
		LoginUser loginUser = (LoginUser)CommonUtil.getParameterObject(LoginUser.class, request);
		loginUser.setUserName(StringUtil.getFromBase64(loginUser.getUserName()));//前台传递的userName是base64编码的保存数据库要转码
		try {
			result = userConfigService.editPassword(loginUser);
		} catch (Exception e) {
			result = JsonUtil.messageJson(false, "服务器异常，密码修改失败");
			log.error("UserConfigController.editPassword error", e);
		}
		return result;
	}
	
	/**
	 * 查看用户名是否重复
	 * @param request
	 * @param session
	 * @param response
	 * @return
	 */
	@RequestMapping(value = "/isRepeat", method = RequestMethod.POST,produces="text/html;charset=UTF-8")
	@ResponseBody
	public String isRepeat(HttpServletRequest request, HttpSession session, HttpServletResponse response) {
		String result="";
		LoginUser loginUser = (LoginUser)CommonUtil.getParameterObject(LoginUser.class, request);
		try {
			List<LoginUser> users = userConfigService.isRepeat(loginUser);
			result = "0";// 代表无重复的用户名
			if (users!=null && users.size() > 0) {
				result = "1";
			}
		} catch (Exception e) {
			log.error("UserConfigController.isRepeat error", e);
		}
		return result;
	}
}
